import { FormSchema } from '@/components/Form';
import { useDictStore } from '@/store/modules/dict';
import { MemberSumma, renderPopoverMemberSumma } from '@/utils';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { cloneDeep } from 'lodash-es';
import { ref } from 'vue';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public storeId = null; // 商家 id
  public bankName = ''; // 银行名称
  public accountNum = ''; // 银行卡号
  public accountName = ''; // 开户名
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 更新时间
  public createdBy = 0; // 创建人
  public createdBySumma?: null | MemberSumma = null; // 创建人摘要信息
  public updatedBy = 0; // 最后更新人
  public updatedBySumma?: null | MemberSumma = null; // 最后更新人摘要信息

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  // {
  //   field: 'id',
  //   component: 'NInputNumber',
  //   label: 'id',
  //   componentProps: {
  //     placeholder: '请输入id',
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'storeName',
    component: 'NInput',
    label: '门店名称',
    componentProps: {
      placeholder: '请输入门店名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'accountNum',
    component: 'NInput',
    label: '银行卡号',
    componentProps: {
      placeholder: '请输入银行卡号',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '创建时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: 80,
  },
  {
    title: '门店名称',
    key: 'storeName',
    align: 'left',
    width: 250,
  },
  {
    title: '银行名称',
    key: 'bankName',
    align: 'left',
    width: 110,
  },
  {
    title: '银行卡号',
    key: 'accountNum',
    align: 'left',
    width: 200,
  },
  {
    title: '开户名',
    key: 'accountName',
    align: 'left',
    width: 100,
  },
  {
    title: '创建人',
    key: 'createdBy',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderPopoverMemberSumma(row.createdBySumma);
    },
  },
  {
    title: '最后更新人',
    key: 'updatedBy',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderPopoverMemberSumma(row.updatedBySumma);
    },
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: 160,
  },
  {
    title: '更新时间',
    key: 'updatedAt',
    align: 'left',
    width: 160,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['storeOption']);
}